package com.jy.wms.service;

import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsWaveMapper;
import com.jy.wms.pojo.WmsShipmentOrder;
import com.jy.wms.pojo.WmsWave;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @ClassName: WmsWaveService.java
* @Description: TODO(这里用一句话描述这个类的作用)
* @Author syh
* @Date Mon Oct 23 15:32:35 CST 2017
*/

@Service
public class WmsWaveService extends BaseService<WmsWaveMapper,WmsWave> {

    /**
     * 新建波次号
     * @param ckid
     * @param owner
     * @return
     * @throws Exception
     */
    public String creatBcBh(String ckid, String owner)throws Exception {
        try {
            String bh = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date time = new Timestamp(System.currentTimeMillis());
            String bcTime = sdf.format(time);
            int number = dao.selectBcNoCountInOneDay(bcTime,ckid,owner);
            if (number != 0) {
                String num = dao.selectBcNumInOneDay(bcTime,ckid,owner);
                String newNum = num.replaceAll("^(0+)", "");
                int bhno = Integer.parseInt(newNum) + 1;
                String str = String.format("%8d", bhno).replace(" ", "0");
                bh = bcTime + "W" + str;
            } else {
                bh = bcTime + "W" + "00000001";
            }
            return bh;
        }catch (Exception e){
            throw new ServiceException("生成波次编号",e);
        }
    }

    /**
     * 添加去重
     * @param bh
     * @param ckid
     * @param sshzid
     * @return
     */

    public WmsWave searchAdd(String bh, String ckid, String sshzid){
        return dao.searchAdd(bh,ckid,sshzid);
    }

    /**
     * 创建波次
     * @param entity
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "波次管理", btnName = "创建波次")
    public void baseInsert(WmsWave entity, List<String> list) throws Exception {
        if(list.size()>0){
            Integer j = 0;
            for (String id : list) {
                //so状态为初始状态才可以进行创建批次(查询发运订单数据)
                WmsShipmentOrder wmsShipmentOrder = dao.searchShipmentOrder(id);
                //so状态为初始状态(0)
                if ("0".equals(wmsShipmentOrder.getFyzt())) {
                    //波次状态（0、未加入 1、已加入）
                    if ("0".equals(wmsShipmentOrder.getBczt())) {
                        //传发运id改波次id
                        j = dao.updateBcid(id, entity.getId());
                        if (j <= 0) {
                            throw new RuntimeException("发运订单的波次号更改失败");
                        }
                    } else {
                        throw new RuntimeException("请选择发运订单波次状态为未加入的数据");
                    }
                } else {
                    throw new RuntimeException("请选择发运订单SO状态为初始的数据");
                }
            }
            j = dao.baseInsert(entity);
            if (j > 0) {
                throw new SucessException("创建波次成功");
            } else {
                throw new RuntimeException("创建波次失败");
            }
        }else{
            throw new RuntimeException("请至少选中一条发运订单数据");
        }
    }


}
